﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;
using System.Windows.Media;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 插入备注文本块命令。
    /// ★①，修改类名。
    /// </summary>
    public static class InsertPictureBoxCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static InsertPictureBoxCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "InsertPictureBoxCommand",
                "InsertPictureBoxCommand",
                typeof(InsertPictureBoxCommand),//创建RoutedUICommand对象
                null);//插入标题图片不给命令。

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            //如何插入一个图片？？？

            AddPictureBox();
        }

        public static void AddPictureBox(Point? point = null)
        {
            Title mainSelectedTitle = null;

            if (Globals.MainWindow.SelectedTitlesList.Count <= 0)
            {
                if (Globals.MainWindow.SelectedTitleImage == null) return;

                mainSelectedTitle = Globals.MainWindow.SelectedTitleImage.MasterTitle;
            }

            foreach (Title t in Globals.SelectedTitlesList)
            {
                if (t.IsMainSelectedTitle)
                {
                    mainSelectedTitle = t; break;
                }
            }

            if (mainSelectedTitle == null) return;
            if (mainSelectedTitle.IsFixed) return;

            Point selCenter = new Point(mainSelectedTitle.LeftTop.X + mainSelectedTitle.ActualWidth / 2,
                mainSelectedTitle.LeftTop.Y + mainSelectedTitle.ActualHeight / 2);

            PictureBox pb = new PictureBox(Globals.MainWindow, mainSelectedTitle);


            XmlNode pbSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("PictureBoxSet");
            if (pbSetNode == null)
            {
                pbSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.AppendXml("<PictureBoxSet />");
            }

            XmlNode newData;

            if (point != null)
            {
                Point pt = (Point)point;

                Point newLocation;

                if (pb.Editor.RootTitle == pb.MasterTitle)
                {
                    newLocation = pt;
                }
                else
                {
                    newLocation = new Point(pt.X - selCenter.X, pt.Y - selCenter.Y);
                }

                pb.Location = newLocation;

                newData = pbSetNode.AppendXml("<PictureBox MasterTitleID=\"" + mainSelectedTitle.ID + "\" Location=\"" + newLocation.ToString() + "\"/>");

            }
            else
            {
                newData = pbSetNode.AppendXml("<PictureBox MasterTitleID=\"" + mainSelectedTitle.ID + "\" />");
            }

            pb.XmlData = newData;
            pb.RebuildANewID();

            Globals.MainWindow.mainCanvas.Children.Add(pb);
            Globals.MainWindow.mainCanvas.Children.Add(pb.ConnectLine);

            ModifingItem mi = new ModifingItem() { ModifingName = "添加图片框" };

            if (pb.MasterTitle != null && pb.MasterTitle == Globals.MainWindow.RootTitle)
            {
                pb.BorderColor = Brushes.DarkCyan;
                pb.HideConnectLine = true;
            }

            Action actAddPictureBox = new Action(pb.ID, -1, pb.GetType().Name, "AddPictureBox", null,
                pb.XmlData.OuterXml, ActionType.AddPictureBox, pb.MasterTitleID);

            mi.Add(actAddPictureBox);

            pb.RefreshLoaction();

            Globals.MainWindow.SelectedPictureBox = pb;
            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。

            if (Globals.SelectedTitlesList.Count != 1)
            {
                e.CanExecute = false; return;
            }

            Title t = Globals.SelectedTitlesList[0];
            if (t.IsFixed)
            {
                e.CanExecute = false; return;
            }
            else
            {
                e.CanExecute = true;
            }
        }
    }
}
